﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.CCApSetLocationsParametersApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Xml;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor;

public class CCApSetLocationsParametersApp
{
  private static Hashtable m_attributesInfoHashTable;
  private static ArrayList m_attributesList;
  private static string m_lastEditedAttributeId = "";
  private TxObjectList m_locationsList;

  static CCApSetLocationsParametersApp()
  {
    CCApSetLocationsParametersApp.m_attributesList = new ArrayList();
    CCApSetLocationsParametersApp.InitHashTable();
  }

  public CCApSetLocationsParametersApp()
  {
    this.m_locationsList = this.GetRoboticLocationsFromSelection();
  }

  public static Hashtable AttributesInfoHashTable
  {
    get => CCApSetLocationsParametersApp.m_attributesInfoHashTable;
  }

  public static string LastEditedAttributeId
  {
    get => CCApSetLocationsParametersApp.m_lastEditedAttributeId;
    set => CCApSetLocationsParametersApp.m_lastEditedAttributeId = value;
  }

  public static ArrayList AttributesList => CCApSetLocationsParametersApp.m_attributesList;

  public TxObjectList RoboticLocations => this.m_locationsList;

  private static void InitHashTable()
  {
    CCApSetLocationsParametersApp.m_attributesInfoHashTable = new Hashtable();
    CCApSetLocationsParametersApp.LoadHashWithDefaultAttributesInfo();
    CCApSetLocationsParametersApp.AddControllerSpecificAttributesInfoToHash();
  }

  private static void LoadHashWithDefaultAttributesInfo()
  {
    CApAttributeInfo capAttributeInfo = new CApAttributeInfo("DEFAULT_COMMENT", "General_Comment", valueType.STRING, displayType.STRING, validLocationType.ALL_TYPES);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_COMMENT", (object) capAttributeInfo);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_COMMENT");
    CApEnumAttributeInfo enumAttributeInfo1 = new CApEnumAttributeInfo("RRS_MOTION_TYPE", "Motion_Motion Type", valueType.INT, displayType.COMBOBOX, validLocationType.ALL_TYPES, new ArrayList()
    {
      (object) new CApEnumAttributeItem("Joint", (object) 1),
      (object) new CApEnumAttributeItem("Linear", (object) 2),
      (object) new CApEnumAttributeItem("Slew", (object) 3),
      (object) new CApEnumAttributeItem("Circular via", (object) 4)
    });
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_RRS_MOTION_TYPE", (object) enumAttributeInfo1);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_RRS_MOTION_TYPE");
    CApNumericAttributeInfo numericAttributeInfo1 = new CApNumericAttributeInfo("RRS_JOINT_SPEED", "Motion_Joint Speed", valueType.DOUBLE, displayType.DOUBLE, validLocationType.ALL_TYPES, 100M, 0M);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_RRS_JOINT_SPEED", (object) numericAttributeInfo1);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_RRS_JOINT_SPEED");
    CApNumericAttributeInfo numericAttributeInfo2 = new CApNumericAttributeInfo("RRS_CARTESIAN_POSITION_SPEED", "Motion_Speed", valueType.DOUBLE, displayType.DOUBLE, validLocationType.ALL_TYPES, 99999M, 0M);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_RRS_MOTION_SPEED", (object) numericAttributeInfo2);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_RRS_MOTION_SPEED");
    CApNumericAttributeInfo numericAttributeInfo3 = new CApNumericAttributeInfo("RRS_MOTION_TIME", "Motion_Time", valueType.DOUBLE, displayType.DOUBLE, validLocationType.ALL_TYPES, 99999M, 0M);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_RRS_MOTION_TIME", (object) numericAttributeInfo3);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_RRS_MOTION_TIME");
    CApEnumAttributeInfo enumAttributeInfo2 = new CApEnumAttributeInfo("RRS_ZONE_NAME", "Motion_Zone", valueType.STRING, displayType.COMBOBOX, validLocationType.VIA, new ArrayList()
    {
      (object) new CApEnumAttributeItem("fine", (object) "fine"),
      (object) new CApEnumAttributeItem("medium", (object) "medium"),
      (object) new CApEnumAttributeItem("coarse", (object) "coarse"),
      (object) new CApEnumAttributeItem("nodecel", (object) "nodecel")
    });
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_RRS_ZONE_NAME", (object) enumAttributeInfo2);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_RRS_ZONE_NAME");
    CApEnumAttributeInfo enumAttributeInfo3 = new CApEnumAttributeInfo("SW_GUN_STATE", "Process_Gun State", valueType.INT, displayType.COMBOBOX, validLocationType.ALL_TYPES, new ArrayList()
    {
      (object) new CApEnumAttributeItem("Open", (object) 3),
      (object) new CApEnumAttributeItem("Semi-Open", (object) 2),
      (object) new CApEnumAttributeItem("Closed", (object) 1),
      (object) new CApEnumAttributeItem("No Change", (object) 4)
    });
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_SW_GUN_STATE", (object) enumAttributeInfo3);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_SW_GUN_STATE");
    CApEnumAttributeInfo enumAttributeInfo4 = new CApEnumAttributeInfo("SW_GUN_WAIT", "Process_Gun Wait", valueType.INT, displayType.COMBOBOX, validLocationType.ALL_TYPES, new ArrayList()
    {
      (object) new CApEnumAttributeItem("Wait", (object) 1),
      (object) new CApEnumAttributeItem("No Wait", (object) 0)
    });
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_SW_GUN_WAIT", (object) enumAttributeInfo4);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_SW_GUN_WAIT");
    CApNumericAttributeInfo numericAttributeInfo4 = new CApNumericAttributeInfo("SW_TIME_ON_PT", "Process_Weld Time", valueType.DOUBLE, displayType.DOUBLE, validLocationType.WELD, 99999M, 0M);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_SW_TIME_ON_PT", (object) numericAttributeInfo4);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_SW_TIME_ON_PT");
    CApNumericAttributeInfo numericAttributeInfo5 = new CApNumericAttributeInfo("SW_WAIT_TIME", "Process_Cool Down Time", valueType.DOUBLE, displayType.DOUBLE, validLocationType.WELD, 99999M, 0M);
    CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) "DEFAULT_SW_WAIT_TIME", (object) numericAttributeInfo5);
    CCApSetLocationsParametersApp.m_attributesList.Add((object) "DEFAULT_SW_WAIT_TIME");
  }

  private static void AddControllerSpecificAttributesInfoToHash()
  {
    try
    {
      XmlDocument xmlDocument = new XmlDocument();
      string filename = TxApplication.CurrentUserDirectory + "PedRobParamColumnData.xml";
      xmlDocument.Load(filename);
      string xpath = "/Root/Section";
      foreach (XmlNode selectNode in xmlDocument.SelectNodes(xpath))
        CCApSetLocationsParametersApp.AddControllerAttributesIntoHash(selectNode);
    }
    catch (Exception ex)
    {
    }
  }

  private static void AddControllerAttributesIntoHash(XmlNode controllerSection)
  {
    foreach (XmlNode selectNode in controllerSection.SelectNodes("Column"))
    {
      try
      {
        if (selectNode.Attributes["Access"].Value == "RW")
        {
          string innerText1 = selectNode.Attributes["ValueType"].InnerText;
          XmlNode displayNode = selectNode.SelectSingleNode("Display");
          string innerText2 = displayNode.Attributes["DisplayType"].InnerText;
          valueType valType = CCApSetLocationsParametersApp.GetValType(innerText1);
          displayType displayType = valType != valueType.FRAME ? CCApSetLocationsParametersApp.GetDisplayType(innerText2) : displayType.FRAME;
          string str = controllerSection.Attributes["SectionName"].Value + "_";
          CApAttributeInfo capAttributeInfo;
          switch (displayType)
          {
            case displayType.INT:
            case displayType.DOUBLE:
              capAttributeInfo = (CApAttributeInfo) CCApSetLocationsParametersApp.CreateNumericInfo(selectNode.Attributes["Id"].Value, str + selectNode.Attributes["Header"].Value, valType, displayType, displayNode);
              break;
            case displayType.COMBOBOX:
              capAttributeInfo = (CApAttributeInfo) CCApSetLocationsParametersApp.CreateEnumInfo(selectNode.Attributes["Id"].Value, str + selectNode.Attributes["Header"].Value, valType, displayType, displayNode);
              break;
            default:
              capAttributeInfo = new CApAttributeInfo(selectNode.Attributes["Id"].Value, str + selectNode.Attributes["Header"].Value, valType, displayType, validLocationType.ALL_TYPES);
              break;
          }
          string key = $"{controllerSection.Attributes["SectionName"].Value}_{selectNode.Attributes["Id"].Value}";
          CCApSetLocationsParametersApp.m_attributesInfoHashTable.Add((object) key, (object) capAttributeInfo);
          CCApSetLocationsParametersApp.m_attributesList.Add((object) key);
        }
      }
      catch (Exception ex)
      {
      }
    }
  }

  private static CApEnumAttributeInfo CreateEnumInfo(
    string id,
    string name,
    valueType valType,
    displayType displayT,
    XmlNode displayNode)
  {
    ArrayList enumItems = new ArrayList();
    foreach (XmlNode selectNode in displayNode.SelectNodes("ComboDef/ElmDef"))
    {
      object val = valType != valueType.INT ? (object) selectNode.Attributes["Value"].Value : (object) int.Parse(selectNode.Attributes["Value"].Value);
      enumItems.Add((object) new CApEnumAttributeItem(selectNode.Attributes["Name"].Value, val));
    }
    return new CApEnumAttributeInfo(id, name, valType, displayT, validLocationType.ALL_TYPES, enumItems);
  }

  private static CApNumericAttributeInfo CreateNumericInfo(
    string id,
    string name,
    valueType valType,
    displayType displayT,
    XmlNode displayNode)
  {
    Decimal minVal = -99999M;
    if (displayNode.Attributes["MinVal"] != null)
      minVal = Decimal.Parse(displayNode.Attributes["MinVal"].Value);
    Decimal maxVal = 99999M;
    if (displayNode.Attributes["MaxVal"] != null)
      maxVal = Decimal.Parse(displayNode.Attributes["MaxVal"].Value);
    return new CApNumericAttributeInfo(id, name, valType, displayT, validLocationType.ALL_TYPES, maxVal, minVal);
  }

  private static valueType GetValType(string vType)
  {
    switch (vType)
    {
      case "string":
        return valueType.STRING;
      case "int":
        return valueType.INT;
      case "double":
        return valueType.DOUBLE;
      case "frame":
        return valueType.FRAME;
      default:
        throw new Exception("Error when parsing PedRobParamColumnData.xml file. UnExisting value type!");
    }
  }

  private static displayType GetDisplayType(string dType)
  {
    switch (dType)
    {
      case "string":
        return displayType.STRING;
      case "int":
        return displayType.INT;
      case "double":
        return displayType.DOUBLE;
      case "frame":
        return displayType.FRAME;
      case "combobox":
        return displayType.COMBOBOX;
      default:
        throw new Exception("Error when parsing PedRobParamColumnData.xml file. UnExisting display type!");
    }
  }

  private TxObjectList GetRoboticLocationsFromSelection()
  {
    return TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
  }

  public string GetCommonStrParamValueFromLocations(string paramName)
  {
    string valueFromLocations = this.GetLocationStrParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0], paramName);
    for (int index = 0; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
    {
      string locationStrParamValue = this.GetLocationStrParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index], paramName);
      if (valueFromLocations != locationStrParamValue)
      {
        valueFromLocations = "";
        break;
      }
    }
    return valueFromLocations;
  }

  private string GetLocationStrParamValue(ITxRoboticOperation robOperation, string paramName)
  {
    string locationStrParamValue = "";
    try
    {
      locationStrParamValue = ((TxRoboticStringParam) robOperation.GetParameter(paramName)).Value;
    }
    catch (Exception ex)
    {
    }
    return locationStrParamValue;
  }

  public string GetCommonCommentParamValueFromLocations()
  {
    string valueFromLocations = "";
    try
    {
      valueFromLocations = this.GetLocationCommentParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0]);
      for (int index = 1; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
      {
        string commentParamValue = this.GetLocationCommentParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index]);
        if (valueFromLocations != commentParamValue)
          valueFromLocations = "";
      }
    }
    catch (Exception ex)
    {
    }
    return valueFromLocations;
  }

  private string GetLocationCommentParamValue(ITxRoboticOperation robOperation)
  {
    return ((ITxOperation) robOperation).Description;
  }

  public bool GetCommonIntParamValueFromLocations(string paramName, ref int paramVal)
  {
    bool valueFromLocations = true;
    try
    {
      paramVal = this.GetLocationIntParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0], paramName);
      for (int index = 1; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
      {
        int locationIntParamValue = this.GetLocationIntParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index], paramName);
        if (paramVal != locationIntParamValue)
        {
          valueFromLocations = false;
          break;
        }
      }
    }
    catch (Exception ex)
    {
      valueFromLocations = false;
    }
    return valueFromLocations;
  }

  private int GetLocationIntParamValue(ITxRoboticOperation robOperation, string paramName)
  {
    return ((TxRoboticIntParam) robOperation.GetParameter(paramName)).Value;
  }

  public double GetCommonDoubleParamValueFromLocations(string paramName)
  {
    double doubleParamValue1 = this.GetLocationDoubleParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0], paramName);
    for (int index = 1; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
    {
      double doubleParamValue2 = this.GetLocationDoubleParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index], paramName);
      if (doubleParamValue1 != doubleParamValue2)
        throw new Exception("Double parameter value is different for robotic locations");
    }
    return doubleParamValue1;
  }

  public bool GetCommonDoubleParamValueFromLocations(string paramName, ref double paramVal)
  {
    bool valueFromLocations = true;
    try
    {
      paramVal = this.GetLocationDoubleParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0], paramName);
      for (int index = 1; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
      {
        double doubleParamValue = this.GetLocationDoubleParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index], paramName);
        if (paramVal != doubleParamValue)
        {
          valueFromLocations = false;
          break;
        }
      }
    }
    catch (Exception ex)
    {
      valueFromLocations = false;
    }
    return valueFromLocations;
  }

  private double GetLocationDoubleParamValue(ITxRoboticOperation robOperation, string paramName)
  {
    return ((TxRoboticDoubleParam) robOperation.GetParameter(paramName)).Value;
  }

  public TxFrame GetCommonFrameParamValueFromLocations(string paramName)
  {
    TxFrame valueFromLocations;
    try
    {
      valueFromLocations = this.GetLocationFrameParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[0], paramName);
      for (int index = 1; index < ((Collection<ITxObject>) this.m_locationsList).Count; ++index)
      {
        TxFrame locationFrameParamValue = this.GetLocationFrameParamValue((ITxRoboticOperation) ((Collection<ITxObject>) this.m_locationsList)[index], paramName);
        if (TxFrame.op_Inequality(valueFromLocations, (ITxObject) locationFrameParamValue))
        {
          valueFromLocations = (TxFrame) null;
          break;
        }
      }
    }
    catch (Exception ex)
    {
      valueFromLocations = (TxFrame) null;
    }
    return valueFromLocations;
  }

  private TxFrame GetLocationFrameParamValue(ITxRoboticOperation robOperation, string paramName)
  {
    return (TxFrame) ((TxRoboticTxObjectParam) robOperation.GetParameter(paramName)).Value;
  }

  public void SetLocationsStrParamValue(string paramName, string paramVal)
  {
    TxRoboticStringParam roboticStringParam = new TxRoboticStringParam(paramName, paramVal);
    foreach (ITxObject roboticLocation in (Collection<ITxObject>) this.RoboticLocations)
      (roboticLocation as ITxRoboticOperation).SetParameter((TxRoboticParam) roboticStringParam);
  }

  public void SetLocationsIntParamValue(string paramName, int paramVal)
  {
    TxRoboticIntParam txRoboticIntParam = new TxRoboticIntParam(paramName, paramVal);
    foreach (ITxObject roboticLocation in (Collection<ITxObject>) this.RoboticLocations)
      (roboticLocation as ITxRoboticOperation).SetParameter((TxRoboticParam) txRoboticIntParam);
  }

  public void SetLocationsDoubleParamValue(string paramName, double paramVal)
  {
    TxRoboticDoubleParam roboticDoubleParam = new TxRoboticDoubleParam(paramName, paramVal);
    foreach (ITxObject roboticLocation in (Collection<ITxObject>) this.RoboticLocations)
      (roboticLocation as ITxRoboticOperation).SetParameter((TxRoboticParam) roboticDoubleParam);
    if (!this.IsSpeedParameter(paramName))
      return;
    this.DeleteOtherSpeedParameters(paramName);
  }

  private bool IsSpeedParameter(string paramName)
  {
    return paramName == "RRS_JOINT_SPEED" || paramName == "RRS_CARTESIAN_POSITION_SPEED" || paramName == "RRS_MOTION_TIME";
  }

  private void DeleteOtherSpeedParameters(string paramName)
  {
    switch (paramName)
    {
      case "RRS_JOINT_SPEED":
        this.RemoveParameterFromLocations("RRS_CARTESIAN_POSITION_SPEED");
        this.RemoveParameterFromLocations("RRS_MOTION_TIME");
        break;
      case "RRS_CARTESIAN_POSITION_SPEED":
        this.RemoveParameterFromLocations("RRS_JOINT_SPEED");
        this.RemoveParameterFromLocations("RRS_MOTION_TIME");
        break;
      case "RRS_MOTION_TIME":
        this.RemoveParameterFromLocations("RRS_JOINT_SPEED");
        this.RemoveParameterFromLocations("RRS_CARTESIAN_POSITION_SPEED");
        break;
    }
  }

  private void RemoveParameterFromLocations(string paramName)
  {
    try
    {
      foreach (ITxObject locations in (Collection<ITxObject>) this.m_locationsList)
        (locations as ITxRoboticOperation).RemoveParameter(paramName);
    }
    catch (Exception ex)
    {
    }
  }

  public void SetLocationsFrameParamValue(string paramName, ITxObject paramVal)
  {
    TxRoboticTxObjectParam roboticTxObjectParam = new TxRoboticTxObjectParam(paramName, paramVal);
    foreach (ITxObject roboticLocation in (Collection<ITxObject>) this.RoboticLocations)
      (roboticLocation as ITxRoboticOperation).SetParameter((TxRoboticParam) roboticTxObjectParam);
  }

  public void SetLocationCommonCommentParamValue(string param)
  {
    foreach (ITxOperation roboticLocation in (Collection<ITxObject>) this.RoboticLocations)
      roboticLocation.Description = param;
  }

  public bool CheckLocationsType(validLocationType locType)
  {
    bool flag = false;
    switch (locType)
    {
      case validLocationType.WELD:
        if (((Collection<ITxObject>) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxWeldLocationOperation)))).Count == ((Collection<ITxObject>) this.m_locationsList).Count)
        {
          flag = true;
          break;
        }
        break;
      case validLocationType.VIA:
        if (((Collection<ITxObject>) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxRoboticViaLocationOperation)))).Count == ((Collection<ITxObject>) this.m_locationsList).Count)
        {
          flag = true;
          break;
        }
        break;
      case validLocationType.GENERIC:
        if (((Collection<ITxObject>) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxGenericRoboticLocationOperation)))).Count == ((Collection<ITxObject>) this.m_locationsList).Count)
        {
          flag = true;
          break;
        }
        break;
      case validLocationType.ALL_TYPES:
        flag = true;
        break;
    }
    return flag;
  }
}
